import request from '@/utils/request';


function getLastYearRange() {
    const endDate = new Date();
    const startDate = new Date();
    startDate.setFullYear(endDate.getFullYear() - 5);

    // 格式化日期为 YYYY-MM-DD HH:mm:ss
    const formatDate = (date) => {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');

        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    };

    return {
        startDate: formatDate(startDate),
        endDate: formatDate(endDate)
    };
}

function getCurrentMonthRange(timezone = 'Asia/Shanghai') {
    // 获取当前月份的第一天 00:00:00
    const startDate = new Date();
    startDate.setDate(1);
    startDate.setHours(0, 0, 0, 0);

    // 获取当前月份的最后一天 23:59:59
    const endDate = new Date();
    endDate.setMonth(endDate.getMonth() + 1);
    endDate.setDate(0);
    endDate.setHours(23, 59, 59, 999);

    // 格式化函数（使用 UTC 转换避免时区影响）
    const formatDateTime = (date) => {
        // 创建带时区偏移的日期副本
        const offsetDate = new Date(date.getTime() - (date.getTimezoneOffset() * 60000));

        // 手动格式化
        return [
            offsetDate.getFullYear(),
            String(offsetDate.getMonth() + 1).padStart(2, '0'),
            String(offsetDate.getDate()).padStart(2, '0')
        ].join('-') + ' ' + [
            String(offsetDate.getHours()).padStart(2, '0'),
            String(offsetDate.getMinutes()).padStart(2, '0'),
            String(offsetDate.getSeconds()).padStart(2, '0')
        ].join(':');
    };

    return {
        start_time: formatDateTime(startDate),
        end_time: formatDateTime(endDate)
    };
}


//公司排名
export function companyPaiming() {
    //const { start_time, end_time } = getCurrentMonthRange();
    const start_time = localStorage.getItem('start_time');
    const end_time = localStorage.getItem('end_time');
    return request({
        url: '/task_statistics/company/sort/?start_time=' + start_time + '&end_time=' + end_time,
        method: 'get',
    })
}
//公司卡片列表
export function companyKapian() {
    //const { start_time, end_time } = getCurrentMonthRange();
    const start_time = localStorage.getItem('start_time');
    const end_time = localStorage.getItem('end_time');
    return request({
        url: '/task_statistics/company/permission_list/?start_time=' + start_time + '&end_time=' + end_time,
        method: 'get',
    })
}
//大屏详情--数据统计

export function companyShujutongji(id) {
    //const { start_time, end_time } = getCurrentMonthRange();
    const start_time = localStorage.getItem('start_time');
    const end_time = localStorage.getItem('end_time');
    return request({
        url: '/task_statistics/data_statistics/?company=' + id + '&start_time=' + start_time + '&end_time=' + end_time,
        method: 'get',
    })
}

//大屏详情--巡检数折线图

export function companyXunjianshu(id) {
    //const { start_time, end_time } = getCurrentMonthRange();
    const start_time = localStorage.getItem('start_time');
    const end_time = localStorage.getItem('end_time');
    return request({
        url: '/task_statistics/routing_inspection/count/?company=' + id + '&start_time=' + start_time + '&end_time=' + end_time,
        method: 'get',
    })
}
//大屏详情--异常数柱状图

export function companyYichangshu(id) {
    //const { start_time, end_time } = getCurrentMonthRange();
    const start_time = localStorage.getItem('start_time');
    const end_time = localStorage.getItem('end_time');
    return request({
        url: '/task_statistics/routing_inspection_error/count/?company=' + id + '&start_time=' + start_time + '&end_time=' + end_time,
        method: 'get',
    })
}
//大屏详情--安全教育总数

export function companyAnquanjiaoyuzongshu(id) {
    //const { start_time, end_time } = getCurrentMonthRange();
    return request({
        url: '/task_statistics/safety_education/count/?company=' + id,
        method: 'get',
    })
}
//大屏详情-- 公司人员

export function gongsirenyuan(id) {
    //const { start_time, end_time } = getCurrentMonthRange();
    return request({
        url: '/task_statistics/employee/count/?company=' + id,
        method: 'get',
    })
}
//大屏详情-- 点位

export function dianwei(id) {
    //const { start_time, end_time } = getCurrentMonthRange();
    return request({
        url: '/task_statistics/point_location/count/?company=' + id,
        method: 'get',
    })
}
// /**右中--报警排名 */
// export const ranking=(param:any={})=>{
//     return GET(indexUrl.rightCenter,param)
// }

//查询公司
export function companyList() {
    return request({
        url: '/company/',
        method: 'get',
    })
}

//创建公司
export function creatCompany(data) {
    return request({
        url: '/company/',
        method: 'post',
        data: data,
    })
}

//查看公司管理员
export function companyAdministrators(id) {
    return request({
        url: `/company/${id}/look_super/`,
        method: 'get',
    })
}

//申请加入
export function joinCompany(id) {
    return request({
        url: `/company/${id}/join`,
        method: 'get',
    })
}

//查看公司员工
export function lookCompanyStaff(countNum, pageNum, data) {
    return request({
        url: `section/operation/query_employee/?size=${countNum}&page=${pageNum}`,
        method: 'post',
        data: data,
    })
}

//查看公司所有员工
export function lookCompanyStaffAll(countNum, pageNum, id) {
    return request({
        url: `/section/${id}/employee/?size=${countNum}&page=${pageNum}`,
        method: 'get',
    })
}

//查看申请加入公司的员工
export function lookAddCompanyStaff(countNum, pageNum) {
    return request({
        url: `/company/operation/look_not_approve/?size=${countNum}&page=${pageNum}`,
        method: 'get',
    })
}

//查看历史审批加入公司的记录
export function lookAddCompanyApproval(countNum, pageNum) {
    return request({
        url: `/company/operation/look_approve/?size=${countNum}&page=${pageNum}`,
        method: 'get',
    })
}

//审批用户加入公司
export function approvalUserCompany(data) {
    return request({
        url: `/company/operation/approve_join/`,
        method: 'post',
        data: data
    })
}

//将用户踢出公司
export function userRemoveCompany(id) {
    return request({
        url: `/company/operation/${id}/remove/`,
        method: 'get',
    })
}